package com.zpc.myvideoserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zpc.myvideoserver.common.util.ResponseJson;
import com.zpc.myvideoserver.common.util.UploadLocalService;
import com.zpc.myvideoserver.entity.Collect;
import com.zpc.myvideoserver.entity.User;
import com.zpc.myvideoserver.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zpc
 * @since 2023/11/03
 */
@RestController
@RequestMapping("/user")
@Api(value = "/user",tags = "用户数据")
public class UserController {
    // @Description : 用户访问数据库服务实例 zpc20 2023/11/4 9:29
    @Autowired
    UserService userService;
    // @Description : 上传文件服务实例 zpc20 2023/11/4 9:30
    @Autowired
    UploadLocalService uploadLocalService;


    /**
     * @Name: getAllUserInfo
     * @Params : []:[]
     * @Description : 获取所用用户信息控制类
     * @Author : zpc20
     * @Date : 2023/11/4 8:57
     * @Return com.zpc.myvideoserver.common.util.ResponseJson<java.util.List<com.zpc.myvideoserver.entity.User>>
     * @Throws
     */
    @ApiOperation(
            value = "查询用户信息接口",
            notes = "查询所有用户信息")
    @PreAuthorize("hasAuthority('admin')")//控制类执行前做校验是否有权限
    @GetMapping("/getAllUserInfo")
    @ResponseBody
    public List<User> getAllUserInfo(){

        List<User> userList = userService.list(Wrappers.lambdaQuery());
        if (!userList.isEmpty()){
            return userList;
            // return new ResponseJson<>("查询成功!", 200, userList);
        }
        // return ResponseJson.FailResponse("该表为空",null);
        return null;
    }


    /**
     * @Name: register
     * @Params : [username, file, password, authorities, email]:[java.lang.String, org.springframework.web.multipart.MultipartFile, java.lang.String, java.lang.String, java.lang.String]
     * @Description : 用户注册接口
     * @Author : zpc20
     * @Date : 2023/11/4 9:27
     * @Return com.zpc.myvideoserver.common.util.ResponseJson<com.zpc.myvideoserver.entity.User>
     * @Throws
     */
    @ApiOperation(
            value = "注册接口",
            notes = "用户注册参数:用户名、密码、邮箱、头像 用户ID不用填写")
    @PostMapping("register")
    public ResponseJson<User> register(
            @RequestParam(value = "username") String username,
            @RequestPart(value = "file") MultipartFile file,
            @RequestParam(value = "password") String password,
            @RequestParam(value ="authorities",defaultValue ="")  String authorities,
            @RequestParam(value = "email") String email
    ){

        // 防止用户名重名
        User hasUser = userService.getUserByName(username);
        if(hasUser==null)
        {
            User user = new User();
            user.setName(username);
            // @Description : 密码加密存储 zpc20 2023/11/4 9:41
            password = new BCryptPasswordEncoder().encode(password);
            user.setPassword(password);
            if(!authorities.isEmpty()){
                user.setAuthorities(authorities);
            }
            user.setEmail(email);
            if (!file.isEmpty()){
                try {
                    String avatar =  uploadLocalService.uploads(file,"Avatar");
                    user.setAvatar(avatar);
                }catch (Exception e){
                    return ResponseJson.FailResponse("上传文件错误!",null);
                }
            }
            // @Description : 持久化到数据库 zpc20 2023/11/4 9:40
            boolean result =  userService.save(user);
            if (result){
                return ResponseJson.SuccessResponse("注册成功!",userService.getUserByName(username));
            }else {
                return ResponseJson.FailResponse("注册失败!",null);
            }

        }else {
            return    ResponseJson.FailResponse("用户名重复,请重新输入",null);
        }
    }

    @PatchMapping("/update")
    @ApiOperation(value = "更新用户数据",notes="根据用户ID更新数据")
    public ResponseJson<User> update(@RequestBody User user){
           user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
           boolean res = userService.update(user,new QueryWrapper<User>().eq("id",user.getId()));
           if(res){
               return ResponseJson.SuccessResponse("更新成功!",userService.getById(user.getId()));
           }else {
               return ResponseJson.FailResponse("更新失败!",null);
           }
    }



}
